package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.UserMappingHotel;

public class UserMappingHotelLogic extends AbstractBL {
	public UserMappingHotelLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	
	public UserMappingHotel GetUserMappingHotel(long id) {
		Key key = KeyFactory.createKey(EntityName.UserMappingHotel, id);
		try {
			Entity lang = datastore.get(key);
			UserMappingHotel c = new UserMappingHotel();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<UserMappingHotel> GetAllUser4Admin(long hotelId)
	{
		Query q = new Query(EntityName.UserMappingHotel);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);

		return GetUserMappingList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public UserMappingHotel GetUserMapping(Entity entity) {
		UserMappingHotel item = new UserMappingHotel();
		item.setEntity(entity);
		return item;
	}
	
	public List<UserMappingHotel> GetUserMappingList(List<Entity> entities) {
		List<UserMappingHotel> list = new ArrayList<UserMappingHotel>();
		if(entities!=null)
		for (Entity e : entities) {
			UserMappingHotel a = new UserMappingHotel();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public UserMappingHotel GetUserMapping(long hotelId,String userId){
		Query q = new Query(EntityName.UserMappingHotel);

		Filter filteruser = new FilterPredicate("userId", FilterOperator.EQUAL,
				userId);
		Filter filterhotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);

		Filter filter = CompositeFilterOperator.and(filteruser, filterhotel);
		
		q.setFilter(filter);
		Entity s = datastore.prepare(q).asSingleEntity() ;
		if(s != null){
			return GetUserMapping(s);
		}else{
			return null;
		}
	}
	
	public List<UserMappingHotel> GetUserMapping(String userId){
		Query q = new Query(EntityName.UserMappingHotel);

		Filter filteruser = new FilterPredicate("userId", FilterOperator.EQUAL,
				userId);

		q.setFilter(filteruser);
		
		return GetUserMappingList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<UserMappingHotel> GetAllUserFromHotel(long hotelId){
		Query q = new Query(EntityName.UserMappingHotel);

		Filter filterhotel = new FilterPredicate("hotelId", FilterOperator.EQUAL,
				hotelId);

		q.setFilter(filterhotel);
		
		return GetUserMappingList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public UserMappingHotel ChangeStatus(long amenId) throws EntityNotFoundException{
		
		UserMappingHotel a = GetUserMappingHotel(amenId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public UserMappingHotel DeleteUserMappingHotel(long id) throws EntityNotFoundException{
		UserMappingHotel t = GetUserMappingHotel(id);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public UserMappingHotel AddUserMapping(long hotelId,String userId,String department,String userlevel) throws EntityNotFoundException{
		UserMappingHotel a = new UserMappingHotel();
		a.setAvaliable(true);
		a.setDepartmentString(department);
		//a.setDisplayId(displayId);
		a.setHotelId(hotelId);
		a.setStatus(true);
		a.setUserlevelString(userlevel);
		a.setUserString(userId);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		
		return a;
	}
	
	public UserMappingHotel UpdateUserMappingHotel(UserMappingHotel u)
	{
		Key key = datastore.put(u.getEntity());
		u.setId(key.getId());
		u.setKey(key);
		return u;
	}
}
